package cn.est.controller;
import cn.est.pojo.Malfunction;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.MalfunctionService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/malfunction")
public class MalfunctionController {

    @Resource
    private MalfunctionService malfunctionService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Malfunction> searchMalfunction(@RequestParam Map<String,Object> param) {
        Page<Malfunction> malfunctionPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            malfunctionPage  = malfunctionService.queryMalfunctionPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return malfunctionPage;
    }

    @RequestMapping("/saveOrUpdateMalfunction")
    @ResponseBody
    public Result saveOrUpdateMalfunction(Malfunction malfunction){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(malfunction.getId())){
                flag=malfunctionService.qdtxAddMalfunction(malfunction);
            }else{
                flag=malfunctionService.qdtxModifyMalfunction(malfunction);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getMalfunctionById")
    @ResponseBody
    public Result getMalfunctionById(Long id){
        Result result= ResultUtils.returnSuccess();
        Malfunction malfunction=null;
        try {
            malfunction=malfunctionService.getMalfunctionById(id);
            result.setData(malfunction);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveMalfunction")
    @ResponseBody
    public Result batchRemoveMalfunction(String ids){
        Integer flag=0;
        try {
            flag=malfunctionService.qdtxBatchDeleteMalfunction(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeMalfunctionById")
    @ResponseBody
    public Result removeMalfunctionById(Long id){
        Integer flag=0;
        try {
            flag=malfunctionService.qdtxDeleteMalfunctionById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
